import QtQuick 1.0
import "../Components/"
import "../Util/Database.js" as Database


//アカウント管理のダイアログ
DialogBase {
    id: _root
    width: calcWidth()
    height: calcHeight(_root)
    x: 0
    y: 0

    dialogTitle: "Account Manage"

    signal addPressed       // 追加ボタンイベント
    signal savePressed      // 保存ボタンイベント
    signal closePressed    // 閉じるイベント


    function viewEvent(){
        _root.forceActiveFocus();

        if(_addingInfo.oauthToken.length > 0 && _addingInfo.oauthTokenSecret.length > 0){
            // 追加された情報を追加
            _list.model.append({"token": _addingInfo.oauthToken
                                   , "token_secret": _addingInfo.oauthTokenSecret
                                   , "screen_name": _addingInfo.screenName
                                   , "user_id": _addingInfo.userId});
        }else{
            // 新規で開いたとき
            _list.model.clear();
            var datas = Database.readAccountData();
            for(var i=0; i<datas.length; i++){
                _list.model.append(datas[i]);
            }
            _list.currentIndex = 0;
        }

        // 読んだら消す
        _addingInfo.clear();
    }

    // 幅計算
    function calcWidth(){
        if(false){//_Device.isMobile){
            return parent.width;
        }else{
            return parent.referenceWidth * 0.9;
        }
    }

    // 高さ計算
    function calcHeight(obj){
//        if(false){// _Device.isMobile){
//            return parent.height
//        }else{
//            var max_height = _items.height + titleHeight + 40;
//    //        for(var i=0; i<obj.children.length; i++){
//    //            if(max_height < obj.children[i].height){
//    //                max_height = obj.children[i].height;
//    //            }
//    //        }
//            if(max_height > _root.parent.height){
//                max_height = _root.parent.height - _root.border.width * 2;
//            }
//            return max_height;
//        }
        return parent.height * 0.8
    }

    // 追加した情報を受け取るエレメント
    property alias addingInfo: _addingInfo
    Item{
        id: _addingInfo
        property string oauthToken: ""
        property string oauthTokenSecret: ""
        property string screenName: ""
        property string userId: ""

        function clear(){
            oauthToken = "";
            oauthTokenSecret = "";
            screenName = "";
            userId = "";
        }
    }

    Keys.onEscapePressed: {
        _addingInfo.clear();
        closePressed();
    }


    Component {
        id: _highlightBar
        Rectangle {
            width: (_list.currentItem === null) ? 0 : _list.currentItem.width
            height: (_list.currentItem === null) ? 0 : _list.currentItem.height
            color: _Setting.itemBkColorSelected
//            y: (_list.currentItem === null) ? 0 : _list.currentItem.y;
        }
    }


    ListView{
        id: _list
        width: _root.width * 0.9
        height: 400 * _root.density
        anchors.top: _root.titleBottom
        anchors.topMargin: 15 * _root.density
        anchors.horizontalCenter: parent.horizontalCenter

        clip: true
        highlight: _highlightBar
        highlightFollowsCurrentItem: true
        highlightMoveDuration: 200

        model: ListModel{
        }

        delegate: Item{
            width: _list.width
            height: _text.paintedHeight * 2 * _root.density
            Text{
                id: _text
                anchors.centerIn: parent
                text: screen_name
                color: "#ffffff"
                font.pixelSize: _Setting.fontPixelSizeDlgMessageM * _root.density
            }
            MouseArea{
                anchors.fill: parent
                onClicked: {
                    _list.currentIndex = index;
                }
            }
        }
    }

    Column{
        id: _items
        width: _root.width * 0.9
//        anchors.top: _list.bottom
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 15 * _root.density
        anchors.horizontalCenter: parent.horizontalCenter
        spacing: 15


        Button{
            id: _add
            text: "Add"
            width: parent.width
            font.pixelSize: _Setting.fontPixelSizeButton * _root.density
            onClicked: {
                _addingInfo.clear();
                addPressed();
            }
        }
        Row{
            width: parent.width
            spacing: 15 * _root.density
            Button{
                id: _copy
                text: "Copy"
                width: (parent.width - parent.spacing) / 2
                font.pixelSize: _Setting.fontPixelSizeButton * _root.density
                onClicked: {
                    var obj = _list.model.get(_list.currentIndex);
                    _list.model.insert(_list.currentIndex+1, obj);
                }
            }
            Button{
                id: _delete
                text: "Delete"
                width: (parent.width - parent.spacing) / 2
                font.pixelSize: _Setting.fontPixelSizeButton * _root.density
                onClicked: {
                    _list.model.remove(_list.currentIndex);
                }
            }
        }
        Row{
            width: parent.width
            spacing: 15 * _root.density
            Button{
                id: _up
                text: "Up"
                width: (parent.width - parent.spacing) / 2
                font.pixelSize: _Setting.fontPixelSizeButton * _root.density
                onClicked: {
                    if(_list.currentIndex > 0){
                        _list.model.move(_list.currentIndex, _list.currentIndex - 1, 1);
                    }
                }
            }
            Button{
                id: _down
                text: "Down"
                width: (parent.width - parent.spacing) / 2
                font.pixelSize: _Setting.fontPixelSizeButton * _root.density
                onClicked: {
                    if(_list.currentIndex < (_list.count - 1)){
                        _list.model.move(_list.currentIndex, _list.currentIndex + 1, 1);
                    }
                }
            }
        }
        Row{
            width: parent.width
            spacing: 15 * _root.density
            Button{
                id: _save
                text: "Save"
                width: (parent.width - parent.spacing) / 2
                font.pixelSize: _Setting.fontPixelSizeButton * _root.density
                onClicked: {
                    var obj;
                    var datas = [];
                    for(var i=0; i<_list.count; i++){
                        obj = _list.model.get(i);
                        datas.push({"number": i
                                       , "token": obj.token
                                       , "token_secret": obj.token_secret
                                       , "screen_name": obj.screen_name
                                       , "user_id": obj.user_id});
                    }
                    Database.saveAccountData(datas);
                    // 最後に閉じる
                    _root.savePressed();
                }
            }
            Button{
                id: _close
                text: "Close"
                width: (parent.width - parent.spacing) / 2
                font.pixelSize: _Setting.fontPixelSizeButton * _root.density
                onClicked: {
                    _addingInfo.clear();
                    _root.closePressed();
                }
            }
        }


    }

}
